import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CommonAppBar extends StatelessWidget implements PreferredSizeWidget {
  final String title;
  final List<Widget>? rightActions;
  final VoidCallback? onBack;
  final bool showBackButton;
  final bool showBottomLine;
  final Color? backgroundColor;

  const CommonAppBar({
    super.key,
    required this.title,
    this.rightActions,
    this.onBack,
    this.showBackButton = true,
    this.showBottomLine = false,
    this.backgroundColor,
  });

  @override
  Widget build(BuildContext context) {
    final displayTitle = title.length > 12
        ? '${title.substring(0, 12)}…'
        : title;
    return CupertinoNavigationBar(
      middle: Text(
        displayTitle,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(fontSize: 17.r, fontWeight: FontWeight.w600),
      ),
      leading: showBackButton
          ? CupertinoButton(
              padding: EdgeInsets.zero,
              onPressed: onBack ?? () => context.router.maybePop(),
              child: const Icon(
                CupertinoIcons.back,
                size: 28,
                color: CupertinoColors.black,
              ),
            )
          : null,
      trailing: rightActions != null
          ? Row(
              mainAxisSize: MainAxisSize.min,
              children: rightActions!
                  .map(
                    (action) => Padding(
                      padding: EdgeInsets.only(left: 16.w),
                      child: action,
                    ),
                  )
                  .toList(),
            )
          : null,
      backgroundColor:
          backgroundColor ??
          CupertinoColors.systemBackground.resolveFrom(context),
      border: showBottomLine
          ? const Border(
              bottom: BorderSide(
                color: CupertinoColors.systemGrey5,
                width: 0.5,
              ),
            )
          : null,
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}
